/**
 * Copyright (c) 2014-2015, Suredy technology Co., Ltd. All rights reserved.
 * 
 * @author ZhangMaoren
 * @since 2015年4月13日
 * @version 0.1
 */
package com.suredy.base.model;

import java.util.HashSet;
import java.util.Set;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.JoinColumn;
import javax.persistence.ManyToOne;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import com.suredy.core.model.BaseModel;

/**
 * @author ZhangMaoren
 *
 */
@Entity
@Table(name = "T_SECURITY_PERMISSION")
public class Permission extends BaseModel {

	/**
	 * 
	 */
	private static final long serialVersionUID = 7135822368851983710L;
	
	
	@ManyToOne(cascade = CascadeType.REFRESH, fetch = FetchType.EAGER)
	@JoinColumn(name = "RESOURCEID")
	private Resource resource;
	
	@Column(name = "RESOURCEURI")
	private String resourceUri; // resource url;
	
	@Column(name = "ACTION")
	private Integer action; // 资源访问方式
	
	@OneToMany(mappedBy = "userPermission", cascade={CascadeType.ALL})  
	private Set<User2Permission> userPermissions = new HashSet<User2Permission>();

	@OneToMany(mappedBy = "rolePermission", cascade={CascadeType.ALL})  
	private Set<Role2Permission> rolePermissions = new HashSet<Role2Permission>();

	
	/**
	 * @return the resource
	 */
	public Resource getResource() {
		return resource;
	}

	/**
	 * @param resource the resource to set
	 */
	public void setResource(Resource resource) {
		this.resource = resource;
	}


	
	/**
	 * @return the userPermissions
	 */
	public Set<User2Permission> getUserPermissions() {
		return userPermissions;
	}

	
	/**
	 * @param userPermissions the userPermissions to set
	 */
	public void setUserPermissions(Set<User2Permission> userPermissions) {
		this.userPermissions = userPermissions;
	}

	
	/**
	 * @return the rolePermissions
	 */
	public Set<Role2Permission> getRolePermissions() {
		return rolePermissions;
	}

	
	/**
	 * @param rolePermissions the rolePermissions to set
	 */
	public void setRolePermissions(Set<Role2Permission> rolePermissions) {
		this.rolePermissions = rolePermissions;
	}

	/**
	 * @return the resourceUri
	 */
	public String getResourceUri() {
		return resourceUri;
	}

	
	/**
	 * @param resourceUri the resourceUri to set
	 */
	public void setResourceUri(String resourceUri) {
		this.resourceUri = resourceUri;
	}

	
	/**
	 * @return the action
	 */
	public Integer getAction(){
		return action;
	}

	
	/**
	 * @param action the action to set
	 */
	public void setAction(Integer action) {
		this.action = action;
	}

	@Override
	public boolean equals(Object o) {
		if (this == o)
			return true;
		if (o == null || getClass() != o.getClass())
			return false;

		Permission p = (Permission) o;

		if (id != null ? !id.equals(p.id) : p.id != null) {
			return false;
		}

		if (resourceUri != null ? !resourceUri.equals(p.resourceUri) : p.resourceUri != null) {
			return false;
		}
		if (action != null ? !action.equals(p.action) : p.action != null) {
			return false;
		}

		return true;
	}

	@Override
	public int hashCode() {
		return ((id != null ? id.hashCode() : 0) + (resourceUri != null ? resourceUri.hashCode() : 0) + (action != null ? action.hashCode() : 0));
	}

	@Override
	public String toString() {
		return "Permisssion[" + "id=" + id + ", resourceUri='" + resourceUri + "\'" + ", action='" + Action.parse(action).getDescription() + "\'" + "]";
	}
}
